<?php
/**
 * Created by PhpStorm.
 * User: EZ
 * Date: 2017/6/28
 * Time: 16:12
 */

namespace app\api\controller;
use think\Controller;
class Base extends Controller{
    public $uid=null;
    public $userInfo=null;
    protected $token="AZDSF87GSD5G7SDKGL52S";
    public function __construct()
    {
        parent::__construct();

    }

    public function isLogin(){
        $this->token = I('get.token','');
        if(empty($this->token)){
            die(json_error('未登录'));
        }
        $user=$this->getUserInfo(array('sig' => $this->token));

        if($user['res']!=1){
            die(json_error('登录失败'));
        }
        $this->userInfo=$user['data'];
        $this->uid=$this->userInfo['id'];
    }

    /**
     * 签名验证
     */
    protected  function sign(){
        $post=$_POST;
        if(empty($post)) die(json_error('参数为空'));
        if(!isset($post['sign']))  die(json_error('签名为空'));
        if(!isset($post['stim'])) die(json_error('缺少请求时间参数'));
        $time=ceil($post['stim']/1000);
        if(($time+120)<time()) die(json_error('请求超时'));
        sort($post);
        $str='';
        foreach($post as $k=>$v){
            if($k!='sign' && !is_array($v) && $v !=''){
                $str.=$k.'='.$v.'&';
            }
        }
        $str=trim($str,'&');
        $test_sign=strtoupper(md5($str.'&'.$this->sign));
        if($post['sign']==$test_sign) {
            die(json_error('签名验证失败'));
        }
    }

    public function send($method,$data=array()){
        $client= \Hprose\Client::create("http://test.7cai.tv/index.php/api/api/user",false);
        $client->setHeader('Authorization',$data['sig']);
        $res = $client->{$method}($data);
        return $res;
    }

    //获取userInfo 信息接口 参数 $sig=['sig'=>'sdfsdfsgsd4s4f4sd']
    public function getUserInfo($sig=array()){
        $result=$this->send('getUserInfo',$sig);
        $res=json_decode($result,true);
        return $res;
    }
    //修改七彩币数量接口 参数 score:增减的七彩币数量 type:增减的七彩币类型 0=减 1=增 sig
    public function modifyScore($score,$type,$sig){
        $data['score']=$score;
        $data['type']=$type;
        $data['sig']=$sig;
        $result=$this->send('modifyScore',$data);
        $res=json_decode($result,true);
        return $result;
    }
    //获取支付订单信息 amount:支付金额 type:支付 subject:商品标题
    public function getPayOrderInfo($amount,$type,$subject,$sig){
        $data['amount']=$amount;
        $data['type']=$type;
        $data['subject']=$subject;
        $data['sig']=$sig;
        $result=$this->send('getPayOrderInfo',$data);
        $res=json_decode($result,true);
        return $result;
    }

}